package com.etennis.utils;

import com.etennis.constants.IOEnum;
import com.etennis.minio.MinioFileClient;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @program: ETennis
 * @ClassName: IOUtils
 * @description:
 * @author: kai
 * @create: 2024-10-27 15:03
 */
@Component
public class IOUtils {
    @Autowired
    private  MinioFileClient fileClient;
    public  String uploadMedia(MultipartFile multipartFile, IOEnum type){
        String result=null;
        try {
            String filePath = generateFilePath(multipartFile.getOriginalFilename(), type.getMsg());
            result=fileClient.putObject(multipartFile,filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(result!=null&&result.length()!=0)
            return result;
        return null;
    }
    public static String generateFilePath(String fileName,String prefix){
        //根据日期生成路径   2022/1/15/
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String datePath = sdf.format(new Date());
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        int index = fileName.lastIndexOf(".");
        String fileType = fileName.substring(index);
        return new StringBuilder().append(prefix).append(datePath).append(uuid).append(fileType).toString();
    }
    public  String   getType(String fileName){
        int index = fileName.lastIndexOf(".");
        String fileType = fileName.substring(index);
        if(fileType.equals(".png")||fileType.equals(".jpg")){
            return "0";
        }
        if(fileType.equals(".mp4")){
            return "1";
        }
        return null;
    }

}
